package com.comp5511.assignment01;


public class Linklist {
	public Linklist() {
		/*------------------------------------------------------
		 * Constructor, initialize the head Node of the Linklist
		 ------------------------------------------------------*/
		this.storeData = null;
		this.Next = null;
	}

	// Being test
	public Linklist getTopNode(Linklist currentTop) {
		/*---------------------------------------------
		 * Check whether Stack is empty by check the top
		 * node of data in linklist is NULL or not 
		 ---------------------------------------------*/
		if (currentTop.getData() == null) {
			// Warning message
			System.out.println("This Stack is empty");
			// maintain currentHead
			return currentTop;
		} else {
			/*-----------------------------------------------------
			 * THE FOLLOW METHOD IS USED WHEN NEED TO
			 * RETURN DATA IN THE TOP NODE
			 *            currentTop.getData();
			 ------------------------------------------------------*/
			currentTop = currentTop.Next;
			return currentTop;
		}
	}

	// Save data in the node by using function, instead of directly access
	public void saveData(String data) {
		this.storeData = data;
	}

	/*------------------------------------------------------------------------
	 * Get node's data function,it can be used to get any node's data within
	 * specific function
	 -------------------------------------------------------------------------*/
	public String getData() {
		// Get this node's data
		return this.storeData;
	}

	/*--------------------------------------------------------------------------
	 * InsertNode Function, every newNode is insert in the linklist to be the 
	 * new head node of the link list , and finally return the newNode's object
	 * to replace the old head's object which save in the currentHead valuable.
	 ---------------------------------------------------------------------------*/

	public Linklist insertNode(Linklist head, String stackData) {
   // Create a new node
		Linklist newNode = new Linklist();
		// Save data in the new node
		newNode.saveData(stackData);
		// New node become the new head of the linklist
		// In this expression,the "head" is previous head of
		// linklist
		newNode.Next = head;
		return newNode;
		}

	// Delete Linklist
	public void deleteList(Linklist head) throws Throwable {
		Linklist bufferNode = null;
		while (head.Next != null) {
			// Save nextNode to the buffer
			bufferNode = head.Next;
			// Due to java have not got a deconstructor(Has Garbage Collector
			// instead), here I use finalize function to delete node Mandatory
			head.finalize();
			// next node become the new head of linklist
			head = bufferNode;
		}
		// Delete the last node
		head.finalize();
	}

	/*--------------------------------------------------------------------------------------------
	 * Show all data in the linklist with counting numbers
	 --------------------------------------------------------------------------------------------*/
	public void showAllLinklist(Linklist head) {
		// Initialize the counter
		int tempCount = 0;
		// When it is not the tail of linklist
		while (head.Next != null) {
			// Print function,show current node's count and data
			System.out.println("This is the " + tempCount
					+ "  in the linklist   " + head.getData());
			// continue to next node
			head = head.Next;
			// increment function of counter
			tempCount++;
		}
	}

	// Parameters, need to use the provided function to access.
	private String storeData;
	private Linklist Next;
}
